package org.example.ui;

import org.example.entity.EmployeeDTO;
import org.example.service.EmployeeService;
import org.example.service.impl.EmployeeServiceImpl;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class EmployeeManagementPanel extends JPanel {
    private JTable employeeTable;
    private JTextField searchField;
    private JButton searchButton;
    private EmployeeService employeeService = new EmployeeServiceImpl();

    public EmployeeManagementPanel() {
        // 设置背景色和布局
        setBackground(new Color(40, 40, 40));
        setLayout(new BorderLayout());

        // 创建并配置顶部面板（包含搜索功能）
        JPanel topPanel = new JPanel();
        topPanel.setBackground(new Color(40, 40, 40));
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        JLabel nameLabel = new JLabel("员工姓名:");
        nameLabel.setFont(new Font("行书", Font.BOLD, 25));
        topPanel.add(nameLabel);

        searchField = new JTextField(15);
        topPanel.add(searchField);

        searchButton = new JButton("搜索");
        topPanel.add(searchButton);

        add(topPanel, BorderLayout.NORTH);

        // 创建表格模型和表格
        String[] columnNames = {"姓名", "职位", "雇佣日期", "薪资", "佣金", "部门", "地点"};
        DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
        employeeTable = new JTable(tableModel);
        employeeTable.setShowHorizontalLines(true);
        employeeTable.setShowVerticalLines(true);
        employeeTable.setGridColor(new Color(220, 220, 220));
        JScrollPane scrollPane = new JScrollPane(employeeTable);

        add(scrollPane, BorderLayout.CENTER);

        // 搜索按钮点击事件处理
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String empNameToSearch = searchField.getText();
                EmployeeDTO employeeDetail = findEmployeeByName(empNameToSearch); // 假设存在此方法来根据名字查找员工
                if (employeeDetail != null) {
                    showEmployeeDetailsDialog(employeeDetail);
                } else {
                    JOptionPane.showMessageDialog(EmployeeManagementPanel.this,
                            "未找到匹配的员工: " + empNameToSearch, "搜索结果",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            }

            private EmployeeDTO findEmployeeByName(String name) {
                // 这里应该有一个实际的数据获取逻辑，例如查询数据库或调用服务。
                // 示例返回值，应替换为实际数据获取逻辑。
                return employeeService.getEmpByName(name); // 或者返回匹配的EmployeeDTO对象
            }

            private void showEmployeeDetailsDialog(EmployeeDTO employee) {
                JDialog dialog = new JDialog((JFrame)null, "员工详情", true);
                dialog.setSize(300, 200);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setLayout(new GridLayout(7, 2));

                dialog.add(new JLabel("姓名:"));
                dialog.add(new JLabel(employee.getEmpName()));
                dialog.add(new JLabel("职位:"));
                dialog.add(new JLabel(employee.getJob()));
                dialog.add(new JLabel("雇佣日期:"));
                dialog.add(new JLabel(employee.getHireDate().toString()));
                dialog.add(new JLabel("薪资:"));
                dialog.add(new JLabel(Double.toString(employee.getSal())));
                dialog.add(new JLabel("佣金:"));
                dialog.add(new JLabel(Double.toString(employee.getComm())));
                dialog.add(new JLabel("部门:"));
                dialog.add(new JLabel(employee.getDeptName()));
                dialog.add(new JLabel("地点:"));
                dialog.add(new JLabel(employee.getDeptLoc()));

                dialog.pack();
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
            }
        });

        // 初始化表格数据
        initializeTableData();
        centerAlignTableColumns();
    }

    private void initializeTableData() {
        // 清除现有数据
        ((DefaultTableModel) employeeTable.getModel()).setRowCount(0);

        // 假设有一个方法可以获取所有的员工列表
        List<EmployeeDTO> employees = loadAllEmployees(); // 应替换为实际的数据加载逻辑

        // 将数据填充到表格
        for (EmployeeDTO employee : employees) {
            ((DefaultTableModel) employeeTable.getModel()).addRow(new Object[]{
                    employee.getEmpName(),
                    employee.getJob(),
                    employee.getHireDate(),
                    employee.getSal(),
                    employee.getComm(),
                    employee.getDeptName(),
                    employee.getDeptLoc()
            });
        }
    }

    private List<EmployeeDTO> loadAllEmployees() {
        // 此处应有从数据库或其他数据源加载员工数据的逻辑
        // 返回一个模拟的员工列表作为示例
        List<EmployeeDTO> employeDTOs = employeeService.getEmployeDTOs();
        return employeDTOs; // 应替换为实际的员工数据列表
    }

    // 在 EmployeeManagementPanel 类中添加以下方法
    private void centerAlignTableColumns() {
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);

        for (int i = 0; i < employeeTable.getColumnCount(); i++) {
            employeeTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
        }
    }
}